package com.msdp.config;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class BloomFilterConfig {
    private final static String FILTER_NAME = "shopBloomFilter";
    private final static Long EXPECTED_INSERTIONS = 1000L;
    private final static Double FALSE_PROBABILITY = 0.01;

    @Resource
    private  RedissonClient redissonClient;


    public RBloomFilter<String> createBloomFilter(String name, long expectedInsertions, double falseProbability) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter(name);
        bloomFilter.tryInit(expectedInsertions, falseProbability);
        return bloomFilter;
    }

    @Bean
    public RBloomFilter<String> bloomFilter(){
        return createBloomFilter(FILTER_NAME, EXPECTED_INSERTIONS, FALSE_PROBABILITY);
    }
}
